package com.example.hrsystem.workflow.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import com.example.hrsystem.R;
import com.example.hrsystem.workflow.WorkflowViewModel;
import com.example.hrsystem.workflow.model.ProcessDefinition;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * 流程定义详情界面Activity
 * 用于创建、编辑和删除流程定义
 */
public class ProcessDefinitionDetailActivity extends AppCompatActivity {
    private WorkflowViewModel workflowViewModel;
    private EditText nameEditText;
    private EditText versionEditText;
    private EditText descriptionEditText;
    private EditText contentEditText;
    private Button saveButton;
    private Button deleteButton;
    private Button cancelButton;
    
    private boolean isNew;
    private int processDefinitionId;
    private ProcessDefinition currentProcessDefinition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_process_definition_detail);

        // 初始化ViewModel
        workflowViewModel = new ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(getApplication())).get(WorkflowViewModel.class);

        // 获取传递的参数
        isNew = getIntent().getBooleanExtra("IS_NEW", true);
        if (!isNew) {
            processDefinitionId = getIntent().getIntExtra("PROCESS_DEFINITION_ID", -1);
        }

        // 初始化UI组件
        initUI();

        // 如果是编辑模式，加载流程定义数据
        if (!isNew && processDefinitionId != -1) {
            loadProcessDefinitionData();
        }
    }

    private void initUI() {
        nameEditText = findViewById(R.id.editText_name);
        versionEditText = findViewById(R.id.editText_version);
        descriptionEditText = findViewById(R.id.editText_description);
        contentEditText = findViewById(R.id.editText_content);
        saveButton = findViewById(R.id.button_save);
        deleteButton = findViewById(R.id.button_delete);
        cancelButton = findViewById(R.id.button_cancel);

        // 设置保存按钮点击事件
        saveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveProcessDefinition();
            }
        });

        // 设置删除按钮点击事件
        deleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteProcessDefinition();
            }
        });

        // 设置取消按钮点击事件
        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        // 如果是新建模式，隐藏删除按钮
        if (isNew) {
            deleteButton.setVisibility(View.GONE);
        }
    }

    private void loadProcessDefinitionData() {
        // 从ViewModel获取流程定义数据
        workflowViewModel.getProcessDefinitionById(processDefinitionId).observe(this, processDefinition -> {
            if (processDefinition != null) {
                currentProcessDefinition = processDefinition;
                nameEditText.setText(processDefinition.getName());
                versionEditText.setText(String.valueOf(processDefinition.getVersion()));
                descriptionEditText.setText(processDefinition.getDescription());
                contentEditText.setText(processDefinition.getDefinitionContent());
            }
        });
    }

    private void saveProcessDefinition() {
        String name = nameEditText.getText().toString().trim();
        String versionStr = versionEditText.getText().toString().trim();
        String description = descriptionEditText.getText().toString().trim();
        String content = contentEditText.getText().toString().trim();

        // 验证输入
        if (name.isEmpty() || versionStr.isEmpty() || content.isEmpty()) {
            Toast.makeText(this, "请填写所有必填字段", Toast.LENGTH_SHORT).show();
            return;
        }

        int version;
        try {
            version = Integer.parseInt(versionStr);
        } catch (NumberFormatException e) {
            Toast.makeText(this, "版本号必须是数字", Toast.LENGTH_SHORT).show();
            return;
        }

        // 创建或更新流程定义
        if (isNew) {
            ProcessDefinition processDefinition = new ProcessDefinition();
            processDefinition.setName(name);
            processDefinition.setVersion(version);
            processDefinition.setDescription(description);
            processDefinition.setDefinitionContent(content);
            processDefinition.setCreatedAt(new Date());
            workflowViewModel.saveProcessDefinition(processDefinition);
        } else {
            currentProcessDefinition.setName(name);
            currentProcessDefinition.setVersion(version);
            currentProcessDefinition.setDescription(description);
            currentProcessDefinition.setDefinitionContent(content);
            workflowViewModel.updateProcessDefinition(currentProcessDefinition);
        }

        Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
        finish();
    }

    private void deleteProcessDefinition() {
        if (!isNew && currentProcessDefinition != null) {
            workflowViewModel.deleteProcessDefinition(currentProcessDefinition.getId());
            Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
            finish();
        }
    }
}